package library;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jdbcutil.JDBCUtil;

public class BookDao {
	
	Connection conn = null;
	PreparedStatement ps = null;
	
	public Integer insert(BookModel model) {
		String sql = "insert into test.book(name,numb,leadcount)values(?,?,?)";
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, model.getName());
			ps.setInt(2, model.getNumb());
			ps.setInt(3, model.getLeadcount());	
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}
	
	public Integer update(BookModel model) {
		StringBuffer sql = new StringBuffer("update test.book");
		List<Object> vals = appendSet(sql,model);
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			for (int i = 0; i < vals.size(); i++) {
				ps.setObject(i + 1, vals.get(i));
			}
			return ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}
	
	private List<Object> appendSet(StringBuffer sql, BookModel model) {
		sql.append(" set name = name ");
		List<Object> objs = new ArrayList<Object>();
		int leadcount = model.getLeadcount();	
		int numb = model.getNumb();
		if (numb != 0) {
			sql.append(",numb = ? ");
			objs.add(numb);
		}
		if (leadcount != 0) {
			sql.append(",leadcount = ? ");
			objs.add(leadcount);
		}
		String name = model.getName();
		sql.append("where name = ? ");
		objs.add(name);
		return objs;
	}
	
	public List<BookModel> selectList(BookModel model) {
		List<BookModel> list = new ArrayList<BookModel>();
		StringBuffer sql = new StringBuffer("select name,numb,leadcount from test.book");
		List<Object> vals = appendWhere(sql, model);
		ResultSet rs = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			if (vals.size() == 0) {
				rs = ps.executeQuery();
			} else {
				for (int i = 0; i < vals.size(); i++) {
					ps.setObject(i + 1, vals.get(i));		
				}
				rs = ps.executeQuery();
			}
			while (rs.next()) {
				BookModel mod = new BookModel();
				mod.setName(rs.getString("name"));
				mod.setNumb(rs.getInt("numb"));
				mod.setLeadcount(rs.getInt("leadcount"));
				list.add(mod);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return list;
	}

	private List<Object> appendWhere(StringBuffer sql, BookModel model) {
		sql.append(" where 1=1");
		List<Object> objs = new ArrayList<Object>();
		String name = model.getName();
		if (name != null && !name.trim().isEmpty()) {// !"".equals(code.trim())
			sql.append(" and name = ?");
			objs.add(name);
		}
		return objs;
	}
	public List<BookModel> appendOrderby(BookModel model) {
		List<BookModel> list = new ArrayList<BookModel>();
		StringBuffer sql = new StringBuffer("select name,numb,leadcount from test.book");
		String orderby = model.getOrderby();
		if (orderby == "t") {// !"".equals(code.trim())
			sql.append(" order by leadcount");
		}
		ResultSet rs = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());	
			rs = ps.executeQuery();
			while (rs.next()) {
				BookModel mod = new BookModel();
				mod.setName(rs.getString("name"));
				mod.setNumb(rs.getInt("numb"));
				mod.setLeadcount(rs.getInt("leadcount"));
				list.add(mod);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.close(conn, ps, rs);
		}
		return list;
	}
	
}
